﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;

namespace WebInvoicingsys.system.sale
{
    public partial class sellbillcheck_list : System.Web.UI.Page
    {
        //前台输出提示信息变量
        public string alert_msg = "";

        //传入的参数
        string start_date, end_date, customer, sabid;

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                start_date = Request.Params["start_date"].ToString();
                end_date = Request.Params["end_date"].ToString();
                customer = Request.Params["customer"].ToString();
                sabid = Request.Params["sabid"].ToString();
            }
            catch
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, Errormsg.er_Parampost);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return false;
            }

            return true;
        }


        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        private DataSet GetData()
        {
            //取得传入的参数
            if (GetParma() == false)
            {
                return null;
            }

            string error_msg = "";

            //构造SQL语句
            string sql = @"select  a.sabid, 
                                   a.sepid, 
                                   a.billdate,
                                   (select warehousename from warehouse where a.warehouseid=warehouse.warehouseid) warehousename,
                                   (select customername from customer where a.customerid=customer.customerid) customername,
                                   a.phone,
                                   a.fax,
                                   (select settlename from settle where a.settleid=settle.settleid) settlename,
                                   a.period,
                                   a.arrivaldate, 
                                   a.marked,
                                   a.statistical,
                                   a.ordercoding,
                                   a.consignee,
                                   a.address,
                                   a.testreport,
                                   a.wayof,
                                   a.payment,
                                   (select sysuser.username from sysuser where a.userid=sysuser.userid) username,
                                   a.remark,
                                   a.sysdate,
                                   a.checkstu,
                                   (select sysuser.username from sysuser where a.checkuserid=sysuser.userid) checkusername,
                                   a.checkdate,
                                   a.checkremark,
                                   a.m_stu,
                                   (select count(c.goodsclassid) from sellbilldetail b, product c where a.sabid=b.sabid and b.productcode=c.productcode) qty1,
                                   (select sum(b.qty) from sellbilldetail b where a.sabid=b.sabid ) qty2,
                                   (select sum(b.qty * b.price) from sellbilldetail b where a.sabid=b.sabid ) qty3
                                from sellbill a
                                  where 1=1";
            
            sql += (start_date == "") ? "" : String.Format(" and a.billdate >= '{0}'", start_date);
            sql += (end_date == "") ? "" : String.Format(" and a.billdate <= '{0}'", end_date);
            sql += (customer == "") ? "" : String.Format(" and a.customerid = '{0}'", customer);
            sql += (sabid == "") ? "" : String.Format(" and a.sepid like '{0}%'", sabid);

            Execute exec = new Execute(Session["connectionString"].ToString());

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (ds == null)
            {
                //显示出错信息
                alert_msg = String.Format(Scripts.alert_warning, error_msg);
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", Scripts.alert_script, true);
                return null;
            }

            DataSet dsResult = new DataSet();

            DataTable dt = new DataTable("List");

            dt.Columns.Add("dgvID");
            dt.Columns.Add("sabid");
            dt.Columns.Add("sepid");
            dt.Columns.Add("billdate");
            dt.Columns.Add("warehousename");
            dt.Columns.Add("customername");
            dt.Columns.Add("phone");
            dt.Columns.Add("fax");
            dt.Columns.Add("settlename");
            dt.Columns.Add("period");
            dt.Columns.Add("arrivaldate");
            dt.Columns.Add("marked");
            dt.Columns.Add("consignee");
            dt.Columns.Add("statistical");
            dt.Columns.Add("ordercoding");
            dt.Columns.Add("address");
            dt.Columns.Add("testreport");
            dt.Columns.Add("wayof");
            dt.Columns.Add("payment");
            dt.Columns.Add("username");
            dt.Columns.Add("remark");
            dt.Columns.Add("sysdate");
            dt.Columns.Add("checkstu");
            dt.Columns.Add("checkusername");
            dt.Columns.Add("checkdate");
            dt.Columns.Add("checkremark");

            dt.Columns.Add("m_stu");
            dt.Columns.Add("qty1");
            dt.Columns.Add("qty2");
            dt.Columns.Add("qty3");


            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dt.NewRow();

                drnew["dgvID"] = (dt.Rows.Count + 1).ToString();
                drnew["sabid"] = dr["sabid"].ToString();
                drnew["sepid"] = dr["sepid"].ToString();
                drnew["billdate"] = dr["billdate"].ToString();
                drnew["warehousename"] = dr["warehousename"].ToString();
                drnew["customername"] = dr["customername"].ToString();
                drnew["phone"] = dr["phone"].ToString();
                drnew["fax"] = dr["fax"].ToString();
                drnew["settlename"] = dr["settlename"].ToString();
                drnew["period"] = dr["period"].ToString();
                drnew["arrivaldate"] = dr["arrivaldate"].ToString();
                drnew["marked"] = dr["marked"].ToString();
                drnew["consignee"] = dr["consignee"].ToString();
                drnew["statistical"] = dr["statistical"].ToString();
                drnew["ordercoding"] = dr["ordercoding"].ToString();
                drnew["address"] = dr["address"].ToString();
                drnew["testreport"] = dr["testreport"].ToString();
                drnew["wayof"] = dr["wayof"].ToString();
                drnew["payment"] = dr["payment"].ToString();
                drnew["username"] = dr["username"].ToString();
                drnew["remark"] = dr["remark"].ToString();
                drnew["sysdate"] = dr["sysdate"].ToString();

                drnew["checkstu"] = drnew["checkstu"] = (dr["checkstu"].ToString() == "0" ? "未审" : "已审");
                drnew["checkusername"] = dr["checkusername"].ToString();
                drnew["checkdate"] = dr["checkdate"].ToString();
                drnew["checkremark"] = dr["checkremark"].ToString();
                drnew["m_stu"] = (dr["m_stu"].ToString() == "0" ? "否" : "是");

                drnew["qty1"] = dr["qty1"].ToString();
                drnew["qty2"] = dr["qty2"].ToString();
                drnew["qty3"] = dr["qty3"].ToString();

                dt.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dt);

            return dsResult;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void bind()
        {
            DataSet ds = GetData();

            if (ds == null)
            {
                return;
            }

            rplist.DataSource = ds.Tables[0];
            rplist.DataBind();
        }

        /// <summary>
        /// 审批
        /// </summary>
        /// <param name="id"></param>
        private void Check(string id)
        {
            if (String.IsNullOrEmpty(id))
            {
                return;
            }

            GetParma();

            string url = String.Format("/system/sale/sellbillcheck_check.aspx?id={0}&start_date={1}&end_date={2}&customer={3}&sabid={4}", id, start_date, end_date, customer, sabid);

            Response.Redirect(url);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //查询
                bind();
            }
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string url = "/system/sale/sellbillcheck.aspx";

            Response.Redirect(url);
        }

        //导出
        protected void btnExport_Click(object sender, EventArgs e)
        {
            //取得数据
            DataSet ds = GetData();

            if (ds == null)
            {
                return;
            }

            //构造导出表头
            String[] headname = { "序号", "计划单号", "销售单号", "开单日期", "发货仓库", "销售客户", "联系电话", "传真", "结算方式", "数期", "到货日期", "标注", 
                                  "收货人", "内部统计码", "内部订单码", "收货地址", "检测报告", "发货方式", "运费支付", "创建人员", "制单日期", "单据备注",
                                  "审核状态", "审核人员", "审核日期", "审核备注", "月结标记", "总类(合计)", "数量(合计)", "金额(合计)" };

            //导出
            ExportToFile.ExportToExcel(ds.Tables[0], HttpContext.Current.Response, headname);
        }

        //审批
        protected void rplist_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();

            switch (e.CommandName)
            {
                case "Check":
                    {
                        Check(id);

                        //查询
                        bind();

                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
    }
}